package com.coldworks.coldjoke.ui;

import java.io.File;

import com.coldworks.base.manager.BaseBitmapManager;
import com.coldworks.base.manager.BaseStorageManager;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.manager.ShareManager;
import com.coldworks.coldjoke.model.TopicModel;
import com.coldworks.coldjoke.util.CONST;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.bean.ShareImage;
import com.umeng.socialize.bean.ULocation;
import com.umeng.socialize.controller.SocializeController;
import com.umeng.socialize.controller.OauthHelper.SHARE_MEDIA;
import com.umeng.socialize.controller.SocializeController.RequestType;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MenuShareTopicView {
	private Context context;
	private PopupWindow mPopup;
	private SocializeController controller = SocializeController.getController(
			"Android", RequestType.SOCIAL);
	private IWXAPI api;
	private TopicModel topicModel;
	private ShareImage shareImage;
	private ULocation uLocation;

	public MenuShareTopicView(Context context) {
		this.context = context;
		api = WXAPIFactory.createWXAPI(context, CONST.APP_ID.WEIXIN_APP_ID);
		initMenu();
	}

	/**
	 * 是否正在显示菜单
	 */
	public boolean isShowing() {
		return false;
	}

	/**
	 * 让菜单消失
	 */
	public void dismiss() {
		if (mPopup != null && !mPopup.isShowing()) {
			mPopup.dismiss();
		}
	}

	public void show(View parent, TopicModel topicModel, ULocation uLocation) {
		this.uLocation = uLocation;
		this.topicModel = topicModel;
		if (!mPopup.isShowing()) {
			mPopup.showAtLocation(parent, Gravity.BOTTOM, 0, 80);
		}
	}

	private void initMenu() {
		LinearLayout vMenu;
		vMenu = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.menu_share, null);

		vMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPopup.dismiss();
			}
		});

		vMenu.findViewById(R.id.menu_share_item_copy).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						ClipboardManager clip = (ClipboardManager) context
								.getSystemService(Context.CLIPBOARD_SERVICE);
						clip.getText(); // 粘贴
						clip.setText(topicModel.getSummary()); // 复制
						Toast.makeText(context, "已成功复制到剪切板", Toast.LENGTH_SHORT)
								.show();
					}
				});

		vMenu.findViewById(R.id.menu_share_item_mms).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						ShareManager.getInstance().shareToMMS(context,
								getShareToPhoneContent(), getShareGatherUrl(),
								true);
					}
				});
		vMenu.findViewById(R.id.menu_share_item_qzone).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.QZONE)) {
							ShareManager.getInstance().shareToQzone(context,
									controller, getShareToWebContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.QZONE,
									getShareToWebContent(), getShareImage(),
									null);
						}
					}
				});
		vMenu.findViewById(R.id.menu_share_item_renren).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.RENREN)) {
							ShareManager.getInstance().shareToRenren(context,
									controller, getShareToWebContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.RENREN,
									getShareToWebContent(), getShareImage(),
									null);
						}
					}
				});
		vMenu.findViewById(R.id.menu_share_item_sina).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.SINA)) {
							ShareManager.getInstance().shareToWeibo(context,
									controller, getShareToWebContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.SINA,
									getShareToWebContent(), getShareImage(),
									null);
						}

					}
				});
		vMenu.findViewById(R.id.menu_share_item_social).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();

						ShareManager.getInstance().shareToWeiXinFriend(context,
								api, getShareToWXGatherUrl(),
								topicModel.getTitle(), topicModel.getSummary(),
								getShareThumbnail());
					}
				});
		vMenu.findViewById(R.id.menu_share_item_t).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.TENCENT)) {
							ShareManager.getInstance().shareToTWeibo(context,
									controller, getShareToWebContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.TENCENT,
									getShareToWebContent(), getShareImage(),
									null);
						}
					}
				});
		vMenu.findViewById(R.id.menu_share_item_weixin).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						ShareManager.getInstance().shareToWeixinPerson(context,
								api, getShareToWXGatherUrl(), null,
								topicModel.getSummary(), getShareThumbnail());
					}
				});

		mPopup = new PopupWindow(vMenu, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mPopup.update();
		mPopup.setFocusable(true);
		mPopup.setAnimationStyle(android.R.style.Animation_Toast);
		mPopup.setBackgroundDrawable(new BitmapDrawable());
	}

	private ShareImage getShareImage() {
		try {
			byte[] bytes = BaseBitmapManager.getInstance().getBytesByFileName(
					BaseStorageManager.getInstance().getImgDir()
							+ File.separator
							+ BaseBitmapManager.getInstance().getFileNameByUrl(
									topicModel.getImgUrl()), 0, -1);
			shareImage = new ShareImage(context, bytes);
			return shareImage;
		} catch (Exception e) {
			return null;
		}
	}

	private String getShareGatherUrl() {
		return "http://lengxiaohua.com/p/gather/" + topicModel.getGatherId();
	}

	private String getShareToWXGatherUrl() {
		return "http://m.lengxiaohua.com/p/gather/" + topicModel.getGatherId();
	}

	private String getShareToPhoneContent() {
		return topicModel.getTitle() + ":" + topicModel.getSummary();
	}

	private String getShareToWebContent() {
		return topicModel.getTitle() + ":" + topicModel.getSummary() + "内容来自于 "
				+ getShareGatherUrl();
	}

	private Bitmap getShareThumbnail() {
		Bitmap bitmap = BaseBitmapManager.getInstance().getThumbnailByPath(
				context,
				BaseStorageManager.getInstance().getImgDir()
						+ File.separator
						+ BaseBitmapManager.getInstance().getFileNameByUrl(
								topicModel.getImgUrl()), 100, 100);
		return bitmap;
	}
}